/*
 * ===================================================
 *
 *       Filename:  parse_track.c
 *
 *
 *        Created:  10/20/2011 05:29:44 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * ===================================================
 */
#include "parser.h"

static int      check_star(char     *parse)
{
  int           cpt = 0;

  for (int i = 0; parse[i] != '\0'; i++)
  {
    if (parse[i] == '*')
      cpt++;
  }
  if (cpt == 1)
    return (1);
  else
    return (0);
}

static char     *get_line(char      *parse,
                          int       *i,
                          int       width)
{
  char          *line = NULL;
  int           j = 0;
  int           nb_l = 0;

  line = malloc(sizeof(char) * MAX_BUFF_SIZE);

  for (; ((parse[*i] != '\n') && (parse[*i] != '\0')); (*i)++)
  {
    line[j] = parse[*i];
    j++;
  }
  line[j + 1] = '\0';
  nb_l++;
  (*i)++;

  if ((mystrlen(line) == width) && (nb_l > 2))
    return (line);
  else if  ((mystrlen(line) > 0) && (nb_l <=2))
    return (line);
  else
    return (NULL);
}

static int      check_line(char     *line)
{
  for (int i = 0; line[i] != '\0'; i++)
  {
    if ((line[i] == 'b') || (line[i] == 'r' ) || (line[i] == 'w')
        || (line[i] == 'g') || (line[i] == '.') ||
        (line[i] == '!') || (line[i] == '*'))
      continue;
    else
    {
      error(3, 0, 0);
      return (3);
    }
  }

  return (0);
}

static char     *get_file(char      *file)
{
  char          *parse = NULL;
  int           fd = 0;
  struct stat   buf;

  if ((fd = open(file, O_RDONLY)) == -1)
    return (NULL);
  else
  {
    fstat(fd, &buf);
    parse = malloc(sizeof(char) * buf.st_size);
    read(fd, parse, buf.st_size);
  }

  return (parse);
}
static void      get_size(int    *width,
                          int    *height,
                          char   *line,
                          int    *i,
                          char   *parse)
{
  line = get_line(parse, i, MAX_BUFF_SIZE);
  *width = my_atoi(line);
  line = get_line(parse, i, *width);
  *height = my_atoi(line);
}

int             is_height_eq(char   *line,
                             char   *parse,
                             int    *i,
                             int    height,
                             int    width)
{
    int         cpt = 0;

    while((line = get_line(parse, i, width)) != NULL)
    {
      cpt++;
      if (check_line(line) == 0)
        continue;
    }
    if (cpt == height)
      return (0);
    else
    {
      error(3, 0, 0);
      return (3);
    }
}
int             parse_track(char    *file)
{
  char          *line = NULL;
  char          *parse = NULL;
  int           height = 0;
  int           width = 0;
  int           i = 0;

  if ((parse = get_file(file)) == NULL)
  {
    error(3, 0, 0);
    return (3);
  }
  get_size(&width, &height, line, &i, parse);
  if (check_star(parse) == 1)
  {
      return is_height_eq(line, parse, &i, height, width);
  }
  else
  {
    error(3, 0, 0);
    return (3);
  }
}


char    **create_map(char   *file)
{
  char  *parse = NULL;
  int   width = 0;
  char  **map = NULL;
  int   height = 0;
  int   i = 0;
  char  *line = NULL;
  int   j = 0;

  parse = get_file(file);
  get_size(&width, &height, line, &i, parse);
  map = malloc(sizeof (char) * height);
  while (j < height)
  {
    map[j] = get_line(parse, &i, width);
    j++;
  }
  return (map);
}

void    start_pos(s_car **car,
                  char  **map)
{
  for (int i = 0; map[i] != NULL; i++)
  {
    for (int j = 0; (map[i][j] != '\0'); j++)
    {
      if (map [i][j] == '*')
      {
        (*car)->x = int_to_fp(i);
        (*car)->y = int_to_fp(j);
      }
    }
  }
}
